import java.io.*;
import java.util.Scanner;

/*
 * @author zzr
 * @date: 2025/04/21  16:36
 * @description: 复制文件操作
 */
public class IO10 {
    public static void main(String[] args) {

        // 1. 输入必要的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件：");
        String srcPath = scanner.next();
        System.out.println("请输入要复制的目标文件：");
        String destPath = scanner.next();

        // 2. 对输入的信息进行检查
        File srcFile = new File(srcPath);
        if (!srcFile.isFile()) {
            System.out.println("输入的源文件有误~");
            return;
        }
        // 目标文件不一定存在，但其父目录应该存在~
        File destFile = new File(destPath);
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("输入的目标文件有误");
        }

        try (InputStream inputStream = new FileInputStream(srcFile);
             OutputStream outputStream = new FileOutputStream(destFile)) {
            byte[] buffer = null;
            while (true) {
                buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (-1 == n) {
                    break;
                }
                outputStream.write(buffer,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
